package com.nhjf.dao.coursePicture;

import com.nhjf.model.coursePicture.CoursePicture;
import com.nhjf.pojo.qo.coursePicture.CoursePictureQo;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.nhjf.common.util.StringUtil;
import com.nhjf.dao.BaseDao;
@Repository
public class CoursePictureDao extends BaseDao<CoursePicture, CoursePictureQo>{

	@Override
	protected Criteria buildCriteria(Criteria criteria, CoursePictureQo qo) {
		// TODO Auto-generated method stub
		if(qo!=null) {
			criteria.setFetchMode("coursesSeries", qo.isSeachCoursesSeries()?FetchMode.JOIN:FetchMode.SELECT);	
			if(qo.getCoursePictureId()!=null) {
				criteria.add(Restrictions.eq("id",qo.getCoursePictureId()));
			}
			if(qo.getCoursesSeriesId()!=null) {
				criteria.add(Restrictions.eq("coursesSeries.id",qo.getCoursesSeriesId()));
			}
			if(StringUtils.isNotBlank(qo.getImageUrl())) {
				criteria.add(Restrictions.eq("imageUrl",qo.getImageUrl()));
			}
			if(qo.getSerialNumber()!=null) {
				criteria.add(Restrictions.eq("serialNumber",qo.getSerialNumber()));
			}
			criteria.addOrder(Order.asc("serialNumber"));
		}
		return criteria;
	}

	@Override
	protected Class<CoursePicture> getEntityClass() {
		// TODO Auto-generated method stub
		return CoursePicture.class;
	}

}
